﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            color.BeginUpdate();
            color.Items.Add("Orange");
            color.Items.Add("red");
            color.Items.Add("green");
            color.Items.Add("blue");
            color.Items.Add("black");
            color.EndUpdate();
            //递归深度
            hScrollBar1.Maximum = 20;
            hScrollBar1.Minimum = 3;
            vScrollBar1.Maximum = 80;
            vScrollBar1.Minimum = 60;
            vScrollBar3.Maximum = 80;
            vScrollBar3.Minimum = 60;
            vScrollBar2.Maximum = 45;
            vScrollBar2.Minimum = 15;
            vScrollBar4.Maximum = 45;
            vScrollBar4.Minimum = 15;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (graphics == null) graphics = this.CreateGraphics();
            this.Refresh();
            drawCayleyTree(hScrollBar1.Value, 400, 400, getLength(), -Math.PI / 2);
        }

        private Graphics graphics;


        int getLength()
        {
            if (radioButton2.Checked)
            {
                return 100;
            }
            else if (radioButton3.Checked)
            {
                return 105;
            }
            else if (radioButton1.Checked)
            {
                return 95;
            }
            else {
                return 90;
            }
        
        }
        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double th1 = vScrollBar4.Value * Math.PI / 180;
            double th2 = vScrollBar2.Value * Math.PI / 180;
            double per1 = vScrollBar3.Value/100.0;
            double per2 = vScrollBar1.Value/100.0;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            switch (color.SelectedItem)
            {
                case "red":
                    graphics.DrawLine(Pens.Red, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "blue":
                    graphics.DrawLine(Pens.Blue, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "green":
                    graphics.DrawLine(Pens.Green, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "black":
                    graphics.DrawLine(Pens.Black, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                default:
                    graphics.DrawLine(Pens.Orange, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
